<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class CheckToken
{
    /**
     * 校验token
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        // $ret =   Redis::set('name1', 'Taylor');
        // $ret =  Redis::get('name1'); 
        // var_dump($ret);DIE;
        $token = Cookie::get('token');
        if (!$token) {
            return response()->json(['message' => '请先登录', 'success' => 'false'], 410);
        }
        $user = DB::table('users')->where('token', $token)->first();
        if (!$user) {
            return response()->json(['message' => '请重新登录', 'success' => 'false'], 410);
        }
        $request->userInfo = $user;
        return $next($request);
    }
}
